
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using System.Web.UI.MobileControls;
using System.Collections.Generic;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Web.Administration.Modules;

namespace Tathata.Web.Administration
{
    public partial class Administration_ProductAdd : BaseNopAdministrationPage
    {
        private void BindData()
        {
            List<ProductCategoryMappingHelperClass> productCategoryMappings = GetProductCategoryMappings(0, string.Empty);
            gvCategoryMappings.DataSource = productCategoryMappings;
            gvCategoryMappings.DataBind();

            List<ProductManufacturerMappingHelperClass> productManufacturerMappings = GetProductManufacturerMappings();
            gvManufacturerMappings.DataSource = productManufacturerMappings;
            gvManufacturerMappings.DataBind();

            List<RelatedProductHelperClass> relatedProducts = GetRelatedProducts();
            gvRelatedProducts.DataSource = relatedProducts;
            gvRelatedProducts.DataBind();
        }

        private void FillDropDowns()
        {
            this.ddlTemplate.Items.Clear();
            ProductTemplateCollection productTemplateCollection = ProductTemplateManager.GetAllProductTemplates();
            foreach (ProductTemplate productTemplate in productTemplateCollection)
            {
                ListItem item2 = new ListItem(productTemplate.Name, productTemplate.ProductTemplateID.ToString());
                this.ddlTemplate.Items.Add(item2);
            }

            this.ddlProductType.Items.Clear();
            ProductTypeCollection productTypeCollection = ProductTypeManager.GetAllProductTypes();
            foreach (ProductType productType in productTypeCollection)
            {
                ListItem item2 = new ListItem(productType.Name, productType.ProductTypeID.ToString());
                this.ddlProductType.Items.Add(item2);
            }

            this.ddlTaxCategory.Items.Clear();
            ListItem itemTaxCategory = new ListItem("Not Taxable", "0");
            this.ddlTaxCategory.Items.Add(itemTaxCategory);
            TaxCategoryCollection taxCategoryCollection = TaxCategoryManager.GetAllTaxCategories();
            foreach (TaxCategory taxCategory in taxCategoryCollection)
            {
                ListItem item2 = new ListItem(taxCategory.Name, taxCategory.TaxCategoryID.ToString());
                this.ddlTaxCategory.Items.Add(item2);
            }

            this.ddlWarehouse.Items.Clear();
            ListItem itemWarehouse = new ListItem("---", "0");
            this.ddlWarehouse.Items.Add(itemWarehouse);
            WarehouseCollection warehouseCollection = WarehouseManager.GetAllWarehouses();
            foreach (Warehouse warehouse in warehouseCollection)
            {
                ListItem item2 = new ListItem(warehouse.Name, warehouse.WarehouseID.ToString());
                this.ddlWarehouse.Items.Add(item2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Download productVariantDownload = null;
                    HttpPostedFile productVariantDownloadFile = fuProductVariantDownload.PostedFile;
                    if ((productVariantDownloadFile != null) && (!String.IsNullOrEmpty(productVariantDownloadFile.FileName)))
                    {
                        byte[] productVariantDownloadBinary = DownloadManager.GetDownloadBits(productVariantDownloadFile.InputStream, productVariantDownloadFile.ContentLength);
                        productVariantDownload = DownloadManager.InsertDownload(productVariantDownloadBinary, productVariantDownloadFile.ContentType, true);
                    }
                    int productVariantDownloadID = 0;
                    if (productVariantDownload != null)
                        productVariantDownloadID = productVariantDownload.DownloadID;

                    DateTime nowDT = DateTime.Now;
                    Product product = ProductManager.InsertProduct(txtName.Text, txtShortDescription.Text, txtFullDescription.Value, txtAdminComment.Text,
                         int.Parse(this.ddlProductType.SelectedItem.Value), int.Parse(this.ddlTemplate.SelectedItem.Value),
                         txtMetaKeywords.Text, txtMetaDescription.Text, txtMetaTitle.Text, txtSEName.Text, cbAllowCustomerReviews.Checked, 0, 0,
                        cbPublished.Checked, false, nowDT, nowDT);

                 
                    foreach (GridViewRow row in gvCategoryMappings.Rows)
                    {
                        CheckBox cbCategoryInfo = row.FindControl("cbCategoryInfo") as CheckBox;
                        HiddenField hfCategoryID = row.FindControl("hfCategoryID") as HiddenField;
                        CheckBox cbFeatured = row.FindControl("cbFeatured") as CheckBox;
                        NumericTextBox txtRowDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;
                        int categoryID = int.Parse(hfCategoryID.Value);
                        int displayOrder = txtRowDisplayOrder.Value;
                        if (cbCategoryInfo.Checked)
                            ProductCategoryManager.InsertProductCategory(product.ProductID, categoryID, cbFeatured.Checked, displayOrder);
                    }

                    foreach (GridViewRow row in gvManufacturerMappings.Rows)
                    {
                        CheckBox cbManufacturerInfo = row.FindControl("cbManufacturerInfo") as CheckBox;
                        HiddenField hfManufacturerID = row.FindControl("hfManufacturerID") as HiddenField;
                        CheckBox cbFeatured = row.FindControl("cbFeatured") as CheckBox;
                        NumericTextBox txtRowDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;
                        int manufacturerID = int.Parse(hfManufacturerID.Value);
                        int displayOrder = txtRowDisplayOrder.Value;
                        if (cbManufacturerInfo.Checked)
                            ProductManufacturerManager.InsertProductManufacturer(product.ProductID, manufacturerID, cbFeatured.Checked, displayOrder);
                    }

                    foreach (GridViewRow row in gvRelatedProducts.Rows)
                    {
                        CheckBox cbProductInfo2 = row.FindControl("cbProductInfo2") as CheckBox;
                        HiddenField hfProductID2 = row.FindControl("hfProductID2") as HiddenField;
                        NumericTextBox txtRowDisplayOrder = row.FindControl("txtDisplayOrder") as NumericTextBox;
                        int productID2 = int.Parse(hfProductID2.Value);
                        int displayOrder = txtRowDisplayOrder.Value;
                        if (cbProductInfo2.Checked)
                            RelatedProductManager.InsertRelatedProduct(product.ProductID, productID2, displayOrder);
                    }

                    Response.Redirect("ProductDetails.aspx?ProductID=" + product.ProductID);
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        public int CategoryID
        {
            get
            {
                return CommonHelper.QueryStringInt("CategoryID");
            }
        }

        public int ProductID
        {
            get
            {
                return CommonHelper.QueryStringInt("ProductID");
            }
        }

        private List<ProductCategoryMappingHelperClass> GetProductCategoryMappings(int ForParentCategoryID, string prefix)
        {
            CategoryCollection categoryCollection = CategoryManager.GetAllCategories(ForParentCategoryID);
            List<ProductCategoryMappingHelperClass> result = new List<ProductCategoryMappingHelperClass>();
            for (int i = 0; i < categoryCollection.Count; i++)
            {
                Category category = categoryCollection[i];

                ProductCategoryMappingHelperClass pcm = new ProductCategoryMappingHelperClass();
                pcm.CategoryID = category.CategoryID;
                pcm.CategoryInfo = prefix + category.Name;
                if (pcm.CategoryID == this.CategoryID)
                    pcm.IsMapped = true;
                result.Add(pcm);
                if (CategoryManager.GetAllCategories(category.CategoryID).Count > 0)
                    result.AddRange(GetProductCategoryMappings(category.CategoryID, prefix + "--"));
            }

            return result;
        }

        private List<ProductManufacturerMappingHelperClass> GetProductManufacturerMappings()
        {
            ManufacturerCollection manufacturerCollection = ManufacturerManager.GetAllManufacturers();
            List<ProductManufacturerMappingHelperClass> result = new List<ProductManufacturerMappingHelperClass>();
            for (int i = 0; i < manufacturerCollection.Count; i++)
            {
                Manufacturer manufacturer = manufacturerCollection[i];

                ProductManufacturerMappingHelperClass pmm = new ProductManufacturerMappingHelperClass();
                pmm.ManufacturerID = manufacturer.ManufacturerID;
                pmm.ManufacturerInfo = manufacturer.Name;
                result.Add(pmm);
            }

            return result;
        }

        private List<RelatedProductHelperClass> GetRelatedProducts()
        {
            ProductCollection productCollection = ProductManager.GetAllProducts();
            List<RelatedProductHelperClass> result = new List<RelatedProductHelperClass>();
            for (int i = 0; i < productCollection.Count; i++)
            {
                Product product = productCollection[i];
                RelatedProductHelperClass rp = new RelatedProductHelperClass();
                rp.ProductID2 = product.ProductID;
                rp.ProductInfo2 = product.Name;
                result.Add(rp);
            }

            return result;
        }

        private class ProductCategoryMappingHelperClass
        {
            public int CategoryID { get; set; }
            public string CategoryInfo { get; set; }
            public bool IsMapped { get; set; }
        }

        private class ProductManufacturerMappingHelperClass
        {
            public int ManufacturerID { get; set; }
            public string ManufacturerInfo { get; set; }
        }

        private class RelatedProductHelperClass
        {
            public int ProductID2 { get; set; }
            public string ProductInfo2 { get; set; }
            public bool IsMapped { get; set; }
        }

    }
}